home *** CD-ROM | disk | FTP | other *** search
/ Stone Design / Stone Design.iso / Stone_Friends / Wave / WavesWorld / Source / IBPalettes / WW3DKit / RIBNuPatch.m < prev    next >
Encoding:
Text File  |  1995-04-19  |  8.2 KB  |  343 lines

  1. // copyright 1993 Michael B. Johnson; some portions copyright 1994, MIT
  2. // see COPYRIGHT for reuse legalities
  3. //
  4.  
  5. #import "RIBNuPatch.h"
  6.  
  7. @implementation RIBNuPatch
  8.  
  9. + initialize { return [RIBNuPatch setVersion:1], self; }
  10.  
  11. - (BOOL)hasBoundingBox { return YES; }
  12.  
  13. - init
  14. {
  15.   [super init];
  16.  
  17.   nU = nV = uOrder = vOrder = 0;
  18.   uKnot = vKnot = NULL;
  19.   uMin  = uMax = vMin = vMax = 0.0;
  20.   nVPlusVOrder = (char *)NXZoneMalloc([self zone], 128);
  21.   nUPlusUOrder = (char *)NXZoneMalloc([self zone], 128);
  22.   tesselationVector[0] = 6.0;
  23.   tesselationVector[1] = 6.0;
  24.  
  25.   return self;
  26. }
  27.  
  28. - awake
  29. {
  30.   [super awake];
  31.  
  32.   return self;
  33. }
  34.  
  35. - free
  36. {
  37.   //if (uKnot) { free(uKnot); }
  38.   //if (vKnot) { free(vKnot); }
  39.   //free(nVPlusVOrder);
  40.   //free(nUPlusUOrder);
  41.  
  42.   return [super free];
  43. }
  44.  
  45. - copyFromZone:(NXZone *)zone
  46. {
  47.    id   newCopy = [super copyFromZone:zone];
  48.  
  49.    NXLogError("WARNING: copyFromZone: incompletely implemented for %s : I'm leaking memory in -free", 
  50.           [[self class] name]);
  51.    return newCopy;
  52. }
  53.  
  54. - setNU:(RtInt)newNU uOrder:(RtInt)newUOrder uKnot:(RtFloat *)newUKnot uMin:(RtFloat)newUMin uMax:(RtFloat)newUMax 
  55.      nV:(RtInt)newNV vOrder:(RtInt)newVOrder vKnot:(RtFloat *)newVKnot vMin:(RtFloat)newVMin vMax:(RtFloat)newVMax
  56.      n:(int)newN tokens:(RtToken *)newTokens parms:(RtPointer *)newParms archiveVector:(char **)newArchiveVector
  57.      printfTypeVector:(int *)newPrintfTypeVector printfNVector:(int *)newPrintfNVector
  58. {  
  59.    nU = newNU;
  60.    nV = newNV;
  61.    uOrder = newUOrder;
  62.    vOrder = newVOrder;
  63.    uKnot = newUKnot; 
  64.    vKnot = newVKnot; 
  65.    uMin = newUMin;
  66.    uMax = newUMax;
  67.    vMin = newVMin;
  68.    vMax = newVMax;
  69.  
  70.    // 
  71.    if (uOrder > 4)
  72.    {  tesselationVector[0] = 1.0;
  73.    }
  74.    if (uOrder == 4)
  75.    {  tesselationVector[0] = 2.0;
  76.    }
  77.    if (uOrder == 3)
  78.    {  tesselationVector[0] = 4.0;
  79.    }
  80.    if (uOrder == 2)
  81.    {  tesselationVector[0] = 6.0;
  82.    }
  83.  
  84.    // 
  85.    if (vOrder > 4)
  86.    {  tesselationVector[1] = 1.0;
  87.    }
  88.    if (vOrder == 4)
  89.    {  tesselationVector[1] = 2.0;
  90.    }
  91.    if (vOrder == 3)
  92.    {  tesselationVector[1] = 4.0;
  93.    }
  94.    if (vOrder == 2)
  95.    {  tesselationVector[1] = 6.0;
  96.    }
  97.  
  98.  
  99.    [self setN:newN tokens:newTokens parms:newParms archiveVector:newArchiveVector printfTypeVector:newPrintfTypeVector printfNVector:newPrintfNVector];
  100.  
  101.    dirtyBoundingBox = TRUE;
  102.    return self;
  103. }
  104.  
  105. - (RtInt)nU {  return nU; }
  106. - (RtInt)uOrder {  return uOrder; }
  107. - (RtFloat *)uKnot {  return uKnot; }
  108. - (RtFloat)uMin {  return uMin; }
  109. - (RtFloat)uMax {  return uMax; }
  110. - (RtInt)nV {  return nV; }
  111. - (RtInt)vOrder {  return vOrder; }
  112. - (RtFloat *)vKnot {  return vKnot; }
  113. - (RtFloat)vMin {  return vMin; }
  114. - (RtFloat)vMax {  return vMax; }
  115.  
  116.  
  117. - (BOOL)theSameAs:otherRIBCommand
  118. {
  119.   int      i;
  120.   RtFloat  *otherUKnot, *otherVKnot;
  121.  
  122.  
  123.   if ([self class] != [otherRIBCommand class])
  124.   {  return NO;
  125.   }
  126.   if (nU != [otherRIBCommand nU])
  127.   {  return NO;
  128.   }
  129.   if (nV != [otherRIBCommand nV])
  130.   {  return NO;
  131.   }
  132.   if (uOrder != [otherRIBCommand uOrder])
  133.   {  return NO;
  134.   }
  135.   if (vOrder != [otherRIBCommand vOrder])
  136.   {  return NO;
  137.   }
  138.   if (uMin != [otherRIBCommand uMin])
  139.   {  return NO;
  140.   }
  141.   if (uMax != [otherRIBCommand uMax])
  142.   {  return NO;
  143.   }
  144.   if (vMin != [otherRIBCommand vMin])
  145.   {  return NO;
  146.   }
  147.   if (vMax != [otherRIBCommand vMax])
  148.   {  return NO;
  149.   }
  150.  
  151.   otherUKnot = [otherRIBCommand uKnot];
  152.   for (i = 0; i < (nU + uOrder); i++) 
  153.   {  if (uKnot[i] != otherUKnot[i])
  154.      {  return NO;
  155.      }
  156.   }
  157.  
  158.   otherVKnot = [otherRIBCommand vKnot];
  159.   for (i = 0; i < (nV + vOrder); i++) 
  160.   {  if (vKnot[i] != otherVKnot[i])
  161.      {  return NO;
  162.      }
  163.   }
  164.  
  165.   return [super theSameAs:otherRIBCommand];
  166. }
  167.  
  168.  
  169. - (BOOL)similarTo:otherRIBCommand 
  170. {
  171.   int      i;
  172.   RtFloat  *otherUKnot, *otherVKnot;
  173.  
  174.  
  175.   if ([self class] != [otherRIBCommand class])
  176.   {  return NO;
  177.   }
  178.   if (nU != [otherRIBCommand nU])
  179.   {  return NO;
  180.   }
  181.   if (nV != [otherRIBCommand nV])
  182.   {  return NO;
  183.   }
  184.   if (uOrder != [otherRIBCommand uOrder])
  185.   {  return NO;
  186.   }
  187.   if (vOrder != [otherRIBCommand vOrder])
  188.   {  return NO;
  189.   }
  190.   if (uMin != [otherRIBCommand uMin])
  191.   {  return NO;
  192.   }
  193.   if (uMax != [otherRIBCommand uMax])
  194.   {  return NO;
  195.   }
  196.   if (vMin != [otherRIBCommand vMin])
  197.   {  return NO;
  198.   }
  199.   if (vMax != [otherRIBCommand vMax])
  200.   {  return NO;
  201.   }
  202.  
  203.   otherUKnot = [otherRIBCommand uKnot];
  204.   for (i = 0; i < (nU + uOrder); i++) 
  205.   {  if (uKnot[i] != otherUKnot[i])
  206.      {  return NO;
  207.      }
  208.   }
  209.  
  210.   otherVKnot = [otherRIBCommand vKnot];
  211.   for (i = 0; i < (nV + vOrder); i++) 
  212.   {  if (vKnot[i] != otherVKnot[i])
  213.      {  return NO;
  214.      }
  215.   }
  216.   return YES;
  217. }
  218.  
  219.  
  220. - renderSelf:(WW3DCamera *)camera startingAt:(RtFloat)shutterOpenTime endingAt:(RtFloat)shutterCloseTime
  221. {
  222.   // due to a bug in QuickRenderMan, the default tesselation values are way too high vis.a.vis. NURBS
  223.   // Therefore, I wrap a call to RiGeometricApproximation here to speed things up...
  224.   RiAttributeBegin();
  225.     RiGeometricApproximation(RI_TESSELATION, RI_PARAMETRIC, tesselationVector, RI_NULL);
  226.     RiNuPatchV(nU, uOrder, uKnot, uMin, uMax, nV, vOrder, vKnot, vMin, vMax, n, tokens, parms);
  227.   RiAttributeEnd();
  228.   return self;
  229. }
  230.  
  231. - (BOOL)isMotionBlurrable { return YES; }
  232.  
  233. - writeEve:(NXStream *)stream atTabLevel:(int)tab
  234. {
  235.    int  i;
  236.  
  237.  
  238.    for (i = 0; i < tab; i++)
  239.    {  NXPrintf(stream, "\t");
  240.    }
  241.    NXPrintf(stream, "NuPatch %d %d {", nU, uOrder);
  242.    for (i = 0; i < (nU + uOrder); i++) 
  243.    {  NXPrintf(stream, "%f ", *(uKnot + i));
  244.    }
  245.    NXPrintf(stream, "} %f %f ",  uMin, uMax);
  246.    NXPrintf(stream, "%d %d {", nV, vOrder);
  247.    for (i = 0; i < (nV + vOrder); i++) 
  248.    {  NXPrintf(stream, "%f ", *(vKnot + i));
  249.    }
  250.    NXPrintf(stream, "} %f %f ",  vMin, vMax);
  251.  
  252.    [super writeParameterList:stream];
  253.    return self;
  254. }
  255.  
  256. - writeInventorAtTime:(float)currentTime to:(NXStream *)stream atTabLevel:(int)tab
  257. {
  258.    int  i, k;
  259.  
  260.  
  261.    for (i = 0; i < tab; i++)
  262.    {  NXPrintf(stream, "\t");
  263.    }
  264.    NXPrintf(stream, "Separator {\n");
  265.  
  266.    // need to print out my points and other stuff here...
  267.    [self writeParameterListForInventor:stream atTabLevel:(tab+1)];
  268.  
  269.    // if ((uMin || vMin) != 0.0) || ((uMax || vMax) != 1.0), I need to dump out st values here... 
  270.  
  271.    for (k = 0; k < (tab+1); k++) {  NXPrintf(stream, "\t"); }
  272.    NXPrintf(stream, "NurbsSurface {\n");
  273.    for (k = 0; k < (tab+2); k++) {  NXPrintf(stream, "\t"); }
  274.    NXPrintf(stream, "numUControlPoints %d\n", nU);
  275.    for (k = 0; k < (tab+2); k++) {  NXPrintf(stream, "\t"); }
  276.    NXPrintf(stream, "numVControlPoints %d\n", nV);
  277.  
  278.    for (k = 0; k < (tab+2); k++) {  NXPrintf(stream, "\t"); }
  279.    NXPrintf(stream, "uKnotVector [");
  280.    for (i=0; i<(nU+uOrder-1); i++)
  281.    {    NXPrintf(stream, "%f, ", uKnot[i]);
  282.    }
  283.    NXPrintf(stream, "%f]\n", uKnot[i]);
  284.  
  285.    for (k = 0; k < (tab+2); k++) {  NXPrintf(stream, "\t"); }
  286.    NXPrintf(stream, "vKnotVector [");
  287.    for (i=0; i<(nV+vOrder-1);  i++)
  288.    {    NXPrintf(stream, "%f, ", vKnot[i]);
  289.    }
  290.    NXPrintf(stream, "%f]\n", vKnot[i]);
  291.    for (k = 0; k < (tab+1); k++) {  NXPrintf(stream, "\t"); }
  292.    NXPrintf(stream, "}\n");
  293.    for (k = 0; k < tab; k++) {  NXPrintf(stream, "\t"); }
  294.    NXPrintf(stream, "}\n");
  295.  
  296.    return self;
  297. }
  298.  
  299.  
  300. #define typeVector "iiiiffff"
  301. #define typeValues &nU, &nV, &uOrder, &vOrder, &uMin, &uMax, &vMin, &vMax
  302.  
  303. - read:(NXTypedStream*)stream 
  304. {
  305.     int version;
  306.  
  307.  
  308.     [super read:stream];
  309.  
  310.     nVPlusVOrder = (char *)malloc(128);
  311.     nUPlusUOrder = (char *)malloc(128);
  312.  
  313. NX_DURING
  314.     version = NXTypedStreamClassVersion(stream,"RIBNuPatch");
  315.     if (version == 0) NXReadTypes(stream,"i",&version), version=1;
  316.     if (version == 1) {
  317.     NXReadTypes(stream, typeVector, typeValues);
  318.     uKnot = (RtFloat *)NXZoneMalloc([self zone], (int)(nU + uOrder)*sizeof(int));
  319.     vKnot = (RtFloat *)NXZoneMalloc([self zone], (int)(nV + vOrder)*sizeof(int));
  320.     NXReadArray(stream, "f", (int)(nU + uOrder), uKnot);
  321.     NXReadArray(stream, "f", (int)(nV + vOrder), vKnot);
  322.     NXReadArray(stream, "f", 2, tesselationVector);
  323.     } 
  324. NX_HANDLER
  325.     NXLogError("in read: %s, exception [%d] raised.\n", 
  326.                [[self class] name], NXLocalHandler.code);
  327.    return nil;
  328. NX_ENDHANDLER
  329.     return self;
  330. }
  331.  
  332. - write:(NXTypedStream*)stream 
  333. {
  334.     [super write:stream];
  335.     NXWriteTypes(stream, typeVector, typeValues);
  336.     NXWriteArray(stream, "f", (int)(nU + uOrder), uKnot);
  337.     NXWriteArray(stream, "f", (int)(nV + vOrder), vKnot);
  338.     NXWriteArray(stream, "f", 2, tesselationVector);
  339.     return self;
  340. }
  341.  
  342. @end
  343.